﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Main_Contact_ContactNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Page.IsPostBack == false)
        {
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"] == "edit")
                {
                    this.btnAddContact.Visible = false;
                    this.btnAddContactNew.Visible = false;
                    this.btnEditContact.Visible = true;
                    this.lblNewContact.Text = "Edit Contact";
                    string contact_id = Request.QueryString["contact_id"];
                    UpdateRecord(contact_id);
                }
            }
            else
            {
                this.btnAddContact.Visible = true;
                this.btnAddContactNew.Visible = true;
                this.btnEditContact.Visible = false;
                this.lblNewContact.Text = "Create New Contact";
            }
        }

    }

    protected void btnAddContact_Click(object sender, EventArgs e)
    {
        insertContact(true);
        
    }
    protected void btnAddContactNew_Click(object sender, EventArgs e)
    {
        insertContact(false);
    }
    protected void insertContact( bool withClose)
    {
        string closeScript = "";
        if (withClose)
        {
            closeScript = "window.close();";
        }
        string command = "sp_insert_contact_info";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
        SqlCommandBuilder.DeriveParameters(sqlCmd);

        sqlCmd.Parameters["@first_name"].Value = this.txtFname.Text;
        sqlCmd.Parameters["@middle_name"].Value = this.txtMname.Text;
        sqlCmd.Parameters["@last_name"].Value = this.txtLname.Text;
        sqlCmd.Parameters["@company_name"].Value = this.txtCompanyName.Text;
        sqlCmd.Parameters["@job_title"].Value = this.txtJobTitle.Text;
        sqlCmd.Parameters["@street"].Value = this.txtStreet.Text;
        sqlCmd.Parameters["@city"].Value = this.txtCity.Text;
        sqlCmd.Parameters["@province"].Value = this.txtProvince.Text;
        sqlCmd.Parameters["@zip_code"].Value = this.txtZipCode.Text;
        sqlCmd.Parameters["@country"].Value = this.txtCountry.Text;
        sqlCmd.Parameters["@home_phone"].Value = this.txtHomePhone.Text;
        sqlCmd.Parameters["@work_phone"].Value = this.txtWorkPhone.Text;
        sqlCmd.Parameters["@mobile"].Value = this.txtMobile.Text;
        sqlCmd.Parameters["@email_address"].Value = this.txtEmail.Text;

        int result = Convert.ToInt32(sqlCmd.ExecuteNonQuery());

        if (result != 0)
        {
            this.RegisterStartupScript("success", "<script language='javascript'>alert('succesfully added contact');window.opener.location.href=window.opener.location.href;window.location.href='ContactNew.aspx'" + closeScript + "</script>");
            
        }
        else
        {
            this.RegisterStartupScript("failed", "<script language='javascript'>alert('failed')</script>");
        }
        sqlCmd.Dispose();
        sqlCon.Close();
    }
    protected void UpdateRecord(string contact_id)
    {
        string command = "sp_getsel_contact";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
        SqlCommandBuilder.DeriveParameters(sqlCmd);
        sqlCmd.Parameters["@contact_id"].Value = new Guid(contact_id.ToString());
        SqlDataReader sqlDr = sqlCmd.ExecuteReader();
        if (sqlDr.HasRows)
        {
            sqlDr.Read();
            this.txtFname.Text = sqlDr["first_name"].ToString();
            this.txtMname.Text = sqlDr["middle_name"].ToString();
            this.txtLname.Text = sqlDr["last_name"].ToString();
            this.txtCompanyName.Text = sqlDr["company_name"].ToString();
            this.txtJobTitle.Text = sqlDr["job_title"].ToString();
            this.txtStreet.Text = sqlDr["street"].ToString();
            this.txtCity.Text = sqlDr["city"].ToString();
            this.txtProvince.Text = sqlDr["province"].ToString();
            this.txtZipCode.Text = sqlDr["zip_code"].ToString();
            this.txtCountry.Text = sqlDr["country"].ToString();
            this.txtHomePhone.Text = sqlDr["home_phone"].ToString();
            this.txtWorkPhone.Text = sqlDr["work_phone"].ToString();
            this.txtMobile.Text = sqlDr["mobile"].ToString();
            this.txtEmail.Text = sqlDr["email_address"].ToString();
        }
        sqlCmd.Dispose();
        sqlDr.Close();
        sqlCon.Close();
    }
    protected void btnEditContact_Click(object sender, EventArgs e)
    {
        string closeScript = "";
        bool withClose = true;
        if (withClose)
        {
            closeScript = "window.close();";
        }
        string contact_id = Request.QueryString["contact_id"];
        string command = "sp_update_contact_info";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
        SqlCommandBuilder.DeriveParameters(sqlCmd);
        sqlCmd.Parameters["@first_name"].Value = this.txtFname.Text;
        sqlCmd.Parameters["@middle_name"].Value = this.txtMname.Text;
        sqlCmd.Parameters["@last_name"].Value = this.txtLname.Text;
        sqlCmd.Parameters["@company_name"].Value = this.txtCompanyName.Text;
        sqlCmd.Parameters["@job_title"].Value = this.txtJobTitle.Text;
        sqlCmd.Parameters["@street"].Value = this.txtStreet.Text;
        sqlCmd.Parameters["@city"].Value = this.txtCity.Text;
        sqlCmd.Parameters["@province"].Value = this.txtProvince.Text;
        sqlCmd.Parameters["@zip_code"].Value = this.txtZipCode.Text;
        sqlCmd.Parameters["@country"].Value = this.txtCountry.Text;
        sqlCmd.Parameters["@home_phone"].Value = this.txtHomePhone.Text;
        sqlCmd.Parameters["@work_phone"].Value = this.txtWorkPhone.Text;
        sqlCmd.Parameters["@mobile"].Value = this.txtMobile.Text;
        sqlCmd.Parameters["@email_address"].Value = this.txtEmail.Text;
        sqlCmd.Parameters["@contact_id"].Value = new Guid(contact_id.ToString());
        int result = Convert.ToInt32(sqlCmd.ExecuteNonQuery());

        if (result != 0)
        {
            this.RegisterStartupScript("success", "<script language='javascript'>alert('succesfully edited contact');window.opener.location.href=window.opener.location.href;" + closeScript + "</script>");
            
        }
        else
        {
            this.RegisterStartupScript("failed", "<script language='javascript'>alert('failed')</script>");
        }
        sqlCmd.Dispose();
        sqlCon.Close();
    }
}
